package com.example.hosptial.service.impl;

import com.example.hosptial.model.enity.Nurse;
import com.example.hosptial.model.enity.NurseWardAssignment;
import com.example.hosptial.model.enity.Ward;
import com.example.hosptial.repository.NurseRepository;
import com.example.hosptial.repository.NurseWardAssignmentRepository;
import com.example.hosptial.repository.WardRepository;
import com.example.hosptial.service.NurseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NurseServiceImpl implements NurseService {

    @Autowired
    private NurseRepository nurseRepository;

    @Autowired
    private NurseWardAssignmentRepository nurseWardAssignmentRepository;

    @Autowired
    private WardRepository wardRepository;

    @Override
    public List<Nurse> getAllNurses() {
        return nurseRepository.findAll();
    }

    @Override
    public List<Nurse> getNursesByDepartment(Integer deptId) {
        return nurseRepository.findByDepartment_DeptId(deptId);
    }

    @Override
    @Transactional
    public Nurse addNurse(Nurse nurse) {
        // 验证护士ID是否已存在
        if (existsByNurseId(nurse.getNurseId())) {
            throw new RuntimeException("护士ID已存在");
        }

        // 验证身份证号是否已存在
        if (existsByNurseIdCard(nurse.getNurseIdCard())) {
            throw new RuntimeException("该身份证号已被注册");
        }

        // 验证电话号码是否已存在
        if (existsByNursePhone(nurse.getNursePhone())) {
            throw new RuntimeException("该电话号码已被注册");
        }

        return nurseRepository.save(nurse);
    }

    @Override
    public boolean existsByNurseIdCard(String nurseIdCard) {
        return nurseRepository.existsByNurseIdCard(nurseIdCard);
    }

    @Override
    public boolean existsByNursePhone(String nursePhone) {
        return nurseRepository.existsByNursePhone(nursePhone);
    }

    @Override
    public boolean existsByNurseId(String nurseId) {
        return nurseRepository.existsByNurseId(nurseId);
    }

    @Override
    public List<Map<String, Object>> getNurseWardAssignments(String nurseId) {
        List<NurseWardAssignment> assignments = nurseRepository.findNurseWardAssignmentsByNurseId(nurseId);
        List<Map<String, Object>> result = new ArrayList<>();
        
        for (NurseWardAssignment assignment : assignments) {
            Map<String, Object> wardInfo = new HashMap<>();
            wardInfo.put("assignmentId", assignment.getAssignmentId());
            wardInfo.put("wardId", assignment.getWard().getWardId());
            wardInfo.put("floorNum", assignment.getWard().getFloor().getFloorNum());
            wardInfo.put("wardNum", assignment.getWard().getWardNum());
            result.add(wardInfo);
        }
        
        return result;
    }

    @Override
    @Transactional
    public void cancelNurseWardAssignment(String nurseId, Integer wardId) {
        nurseWardAssignmentRepository.deleteByNurseIdAndWardId(nurseId, wardId);
    }

    @Override
    public List<Integer> getFloorsByDepartment(Integer deptId) {
        return nurseRepository.findDistinctFloorsByDepartmentId(deptId);
    }

    @Override
    public List<Map<String, Object>> getWardsByFloor(Integer deptId, Integer floorNum) {
        List<Ward> wards = wardRepository.findByFloorFloorNumOrderByWardNum(floorNum);
        List<Map<String, Object>> result = new ArrayList<>();
        
        for (Ward ward : wards) {
            // 只返回属于该科室的病房
            if (ward.getFloor().getDepartment().getDeptId().equals(deptId)) {
                Map<String, Object> wardInfo = new HashMap<>();
                wardInfo.put("wardId", ward.getWardId());
                wardInfo.put("wardNum", ward.getWardNum());
                result.add(wardInfo);
            }
        }
        
        return result;
    }

    @Override
    @Transactional
    public void assignWardToNurse(String nurseId, Integer wardId) {
        // 检查是否已经存在该分配
        if (nurseWardAssignmentRepository.existsByNurseNurseIdAndWardWardId(nurseId, wardId)) {
            throw new RuntimeException("该护士已分配到此病房");
        }

        Nurse nurse = nurseRepository.findById(nurseId)
                .orElseThrow(() -> new RuntimeException("护士不存在"));
        
        Ward ward = wardRepository.findById(wardId)
                .orElseThrow(() -> new RuntimeException("病房不存在"));

        // 检查病房是否属于护士所在的科室
        if (!ward.getFloor().getDepartment().getDeptId().equals(nurse.getDepartment().getDeptId())) {
            throw new RuntimeException("不能分配到其他科室的病房");
        }

        NurseWardAssignment assignment = new NurseWardAssignment();
        assignment.setNurse(nurse);
        assignment.setWard(ward);
        
        nurseWardAssignmentRepository.save(assignment);
    }
} 